﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Security.Cryptography;
using System.Text;

namespace NJL.Web.AdminEnterprise
{
    public partial class EnterPrisePersonalInformation : BasePage
    {
        private readonly BLL.NJL_CS_COMPANY_TYPE bllCompanyType = new BLL.NJL_CS_COMPANY_TYPE();
        private readonly BLL.NJL_CS_SERVICE_TYPE bllServiceType = new BLL.NJL_CS_SERVICE_TYPE();
        private readonly BLL.NJL_USER bllUser = new BLL.NJL_USER();
        private readonly BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();

        Model.NJL_USER modelUser;
        NJL.Model.NJL_COMPANY modelCompany;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session["Token"])
            {
                SetToken();
            }
            if (!IsPostBack)
            {                
                bind();
            }
        }
        public string GetToken()
        {
            if (null != Session["Token"])
            {
                return Session["Token"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        private void SetToken()
        {
            Session.Add("Token", UserMd5(Session.SessionID + DateTime.Now.Ticks.ToString()));
        }

        protected string UserMd5(string str1)
        {
            string cl1 = str1;
            string pwd = "";
            MD5 md5 = MD5.Create();
            // 加密后是一个字节类型的数组 
            byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(cl1));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得 
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
                pwd = pwd + s[i].ToString("X");
            }
            return pwd;
        }
        void bind()
        {
            ddlType.DataSource = bllCompanyType.GetAllList();
            ddlType.DataTextField = "TYPE_NAME";
            ddlType.DataValueField = "TYPE_CODE";
            ddlType.DataBind();

            modelUser = bllUser.GetModel(UserId);
            txtUserName.Text = modelUser.USER_NAME;
            txtTel.Text = modelUser.TEL;
            txtMobel.Text = modelUser.MOBILE;
            txtEmail.Text = modelUser.EMAIL;
            txtQQ.Text = modelUser.QQ;
            txtMsn.Text = modelUser.MSN;

            modelCompany = bllCompany.GetCompanyByUserIDModel(UserId);
            if (modelCompany != null)
            {
                txtEPName.Text = modelCompany.COMPANY_NAME;
                ddlType.Items.FindByValue(modelCompany.TYPE_CODE).Selected = true;

                txtEPAddress.Text = modelCompany.ADDRESS;
                txtEPLinkman.Text = modelCompany.CONTACT;
                txtEPTel.Text = modelCompany.TEL;
                txtEPCellPhone.Text = modelCompany.MOBILE;
                txtEPQQ.Text = modelCompany.QQ;
                txtEPMsn.Text = modelCompany.MSN;
                if (!string.IsNullOrEmpty(modelCompany.IMAGE_CODE))
                {
                    Image1.ImageUrl = GetImageUrl(modelCompany.IMAGE_CODE,"C2");
                }
                else
                {
                    Image1.ImageUrl = "~/Images/defaultShopHead.jpg";
                }

                txtBEST_SIGN.Text = modelCompany.BEST_SIGN;
                txtTRAFFIC_GUIDE.Text = modelCompany.TRAFFIC_GUIDE;
                txtCOMPANY_INTRO.Text = modelCompany.COMPANY_INTRO;

                txtCOMPANY_DESCRIPTION.Text = modelCompany.COMPANY_DESCRIPTION;
            }

            //服务
            //DataSet ds = bllServiceType.GetServicesTypeListByCompanyID(modelCompany.ID);
            //if (ds != null && ds.Tables.Count>0)
            //{
            //    cblServiceProject.DataSource = ds.Tables[0];
            //    cblServiceProject.DataTextField = "TYPE_NAME";
            //    cblServiceProject.DataValueField = "TYPE_CODE";
            //    cblServiceProject.DataBind();
            //    cblServiceProject.Enabled = false;
            //    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            //    {
            //        cblServiceProject.Items[i].Selected = true;
            //    }
            //}
            //cblServiceProject.Enabled = false;
        }

        protected void btnSumbit_Click(object sender, EventArgs e)
        {
            modelUser = bllUser.GetModel(UserId);
            modelUser.USER_NAME = txtUserName.Text.Trim();
            modelUser.TEL = txtTel.Text.Trim();
            modelUser.MOBILE = txtMobel.Text.Trim();
            modelUser.EMAIL = txtEmail.Text.Trim();
            modelUser.QQ = txtQQ.Text.Trim();
            modelUser.MSN = txtMsn.Text.Trim();

            if (bllUser.Update(modelUser))
            {
                this.PersonalInfo.Style.Add("display", "block");
                this.EnterPriseInfo.Style.Add("display", "none");
                this.ShopInfo.Style.Add("display", "none");
                this.ShopDescription.Style.Add("display", "none");

                PersonalInfoCss.Attributes["class"] = "now";
                EnterPriseInfoCss.Attributes["class"] = "";
                ShopInfoCss.Attributes["class"] = "";
                ShopDescriptionCss.Attributes["class"] = "";

                Royalsoft.Common.MessageBox.Show(this, "基本信息修改成功！");
            }
        }

        protected void bntEPSubmit_Click(object sender, EventArgs e)
        {
            modelCompany = bllCompany.GetCompanyByUserIDModel(UserId);
            modelCompany.COMPANY_NAME = txtEPName.Text.Trim();
            modelCompany.TYPE_CODE = ddlType.SelectedValue;

            modelCompany.ADDRESS = txtEPAddress.Text.Trim();
            modelCompany.CONTACT = txtEPLinkman.Text.Trim();
            modelCompany.TEL = txtEPTel.Text.Trim();
            modelCompany.MOBILE = txtEPCellPhone.Text.Trim();
            modelCompany.MSN = txtEPMsn.Text.Trim();
            modelCompany.QQ = txtEPQQ.Text.Trim();

            modelCompany.UPDATE_DATE_TIME = DateTime.Now;
            modelCompany.UPDATE_USER_ID = UserId;
            modelCompany.UPDATE_USER_NAME = UserName;

            if (bllCompany.Update(modelCompany))
            {
                this.PersonalInfo.Style.Add("display", "none");
                this.EnterPriseInfo.Style.Add("display", "block");
                this.ShopInfo.Style.Add("display", "none");
                this.ShopDescription.Style.Add("display", "none");

                PersonalInfoCss.Attributes["class"] = "";
                EnterPriseInfoCss.Attributes["class"] = "now";
                ShopInfoCss.Attributes["class"] = "";
                ShopDescriptionCss.Attributes["class"] = "";

                Royalsoft.Common.MessageBox.Show(this, "企业信息修改成功！");
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (!Request.Form.Get("hiddenTestN").Equals(GetToken()))
            {
                return;
            }
            SetToken();
            modelCompany = bllCompany.GetCompanyByUserIDModel(UserId);
            if (FileUpload1.HasFile)
            {
                if (!string.IsNullOrEmpty(modelCompany.IMAGE_CODE))
                {
                    DeleteImageByCode(modelCompany.IMAGE_CODE);
                }
                if (modelCompany.IMAGE_CODE == "")
                {
                    string message;
                    if (HandleIntegral(UserId, "1", out message))
                    {

                    }
                }
                modelCompany.IMAGE_CODE = SaveUploadFile(Request.Files);
                if (bllCompany.Update(modelCompany))
                {
                    if (modelCompany.IMAGE_CODE == "")
                    {
                       
                    }
                    if (!string.IsNullOrEmpty(modelCompany.IMAGE_CODE))
                    {
                        Image1.ImageUrl = GetImageUrl(modelCompany.IMAGE_CODE);
                    }
                    else
                    {
                        Image1.Visible = false;
                    }
                    this.PersonalInfo.Style.Add("display", "none");
                    this.EnterPriseInfo.Style.Add("display", "none");
                    this.ShopInfo.Style.Add("display", "block");
                    this.ShopDescription.Style.Add("display", "none");
                    PersonalInfoCss.Attributes["class"] = "";
                    EnterPriseInfoCss.Attributes["class"] = "";
                    ShopInfoCss.Attributes["class"] = "now";
                    ShopDescriptionCss.Attributes["class"] = "";
                   
                    Royalsoft.Common.MessageBox.Show(this,"上传成功");
                    Page.ClientScript.RegisterStartupScript(GetType(), "style", "<script>hideEnterPriseinfo('#ShopInfo','#ShopInfoCss')</script>");
                }
            }
            else
            {
                this.PersonalInfo.Style.Add("display", "none");
                this.EnterPriseInfo.Style.Add("display", "none");
                this.ShopInfo.Style.Add("display", "block");
                this.ShopDescription.Style.Add("display", "none");

                PersonalInfoCss.Attributes["class"] = "";
                EnterPriseInfoCss.Attributes["class"] = "";
                ShopInfoCss.Attributes["class"] = "now";
                ShopDescriptionCss.Attributes["class"] = "";
                Royalsoft.Common.MessageBox.Show(this, "请选择要修改的图片！");
            }
        }

        protected void btnSumbitShopInfo_Click(object sender, EventArgs e)
        {
            modelCompany = bllCompany.GetCompanyByUserIDModel(UserId);
            modelCompany.BEST_SIGN = txtBEST_SIGN.Text.Trim();
            modelCompany.COMPANY_INTRO = txtCOMPANY_INTRO.Text.Trim();
            modelCompany.TRAFFIC_GUIDE = txtTRAFFIC_GUIDE.Text;
            if (bllCompany.Update(modelCompany))
            {
                this.PersonalInfo.Style.Add("display", "none");
                this.EnterPriseInfo.Style.Add("display", "none");
                this.ShopInfo.Style.Add("display", "block");
                this.ShopDescription.Style.Add("display", "none");

                PersonalInfoCss.Attributes["class"] = "";
                EnterPriseInfoCss.Attributes["class"] = "";
                ShopInfoCss.Attributes["class"] = "now";
                ShopDescriptionCss.Attributes["class"] = "";

                Royalsoft.Common.MessageBox.Show(this, "店铺资料修改成功！");
            }
        }
        protected void btnSubmitDescription_Click(object sender, EventArgs e)
        {
            modelCompany = bllCompany.GetCompanyByUserIDModel(UserId);

            modelCompany.COMPANY_DESCRIPTION = txtCOMPANY_DESCRIPTION.Text;

            if (bllCompany.Update(modelCompany))
            {
                this.PersonalInfo.Style.Add("display", "none");
                this.EnterPriseInfo.Style.Add("display", "none");
                this.ShopInfo.Style.Add("display", "none");
                this.ShopDescription.Style.Add("display", "block");

                PersonalInfoCss.Attributes["class"] = "";
                EnterPriseInfoCss.Attributes["class"] = "";
                ShopInfoCss.Attributes["class"] = "";
                ShopDescriptionCss.Attributes["class"] = "now";

                Royalsoft.Common.MessageBox.Show(this, "店铺介绍修改成功！");
            }
        }
    }
}